import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/widgets/form_item_field.dart';

class FormItemSelect extends StatelessWidget {
  const FormItemSelect({
    Key? key,
    this.height = 300,
    this.icon = Icons.keyboard_arrow_right,
    required this.lable,
    required this.options,
    required this.value,
    required this.onChange,
  }) : super(key: key);
  final String lable;
  final IconData? icon;
  final List<String> options;
  final int value;
  final Function onChange;
  final double height;

  @override
  Widget build(BuildContext context) {
    // !选中的值
    var controller = FixedExtentScrollController(initialItem: value);
    _showCupertinoActionSheet() {
      showCupertinoModalPopup(
          context: context,
          builder: (context) {
            return Container(
                color: Colors.grey[200],
                height: height,
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        TextButton(
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                            child: const Text('取消')),
                        TextButton(
                            onPressed: () {
                              Navigator.of(context).pop(
                                controller.selectedItem,
                              );
                            },
                            child: const Text('确定')),
                      ],
                    ),
                    Expanded(
                      child: CupertinoPicker(
                        scrollController: controller,
                        itemExtent: 40,
                        onSelectedItemChanged: (v) {},
                        children: options.map((e) => Text(e)).toList(),
                      ),
                    )
                  ],
                ));
          }).then((selectedValue) {
        if (selectedValue != value) {
          onChange(selectedValue);
        }
      });
    }

    return CommonFormItemField(
      lable: lable,
      contentBuilder: (context) {
        return Expanded(
          child: GestureDetector(
            behavior: HitTestBehavior.translucent, //!点击空白处也可以响应事件
            onTap: () {
              _showCupertinoActionSheet();
            },
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [Text(options[value]), Icon(icon)],
            ),
          ),
        );
      },
    );
  }
}
